//
//  VC6CoreAnimation.swift
//  ChapterAll
//
//  Created by Kevin on 2020/4/26.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VC6CoreAnimation: UIViewController {
    
    private lazy var redView = {()->UIView in
        let w:CGFloat = 100
        let x = (view.frame.width - 100) * 0.5
        let ui = UIView(frame: CGRect(x: x, y: 200, width: w, height: w))
        ui.backgroundColor = UIColor.red
        return ui
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(redView)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        test9()
    }
    
    func test9() -> Void {
        let animation1 = CABasicAnimation(keyPath: "backgroundColor")
        animation1.toValue = UIColor.orange.cgColor
        
        let animation2 = CABasicAnimation(keyPath: "position")
        animation2.toValue = CGPoint(x: view.frame.width * 0.5, y: 400)
        
        let animation = CAAnimationGroup()
        animation.duration = 3
        animation.animations = [animation1,animation2]
        redView.layer.add(animation, forKey: nil)
    }
    
    func test8() -> Void {
        let transtion = CATransition()
        transtion.type = .push
        transtion.subtype = .fromTop
        transtion.duration = 3
        redView.layer.add(transtion, forKey: nil)
    }
    
    func test7() -> Void {
        let animation = CAKeyframeAnimation(keyPath: "position")
        animation.values = [CGPoint(x: 300, y: 300),CGPoint(x: 300, y: 600),CGPoint(x: 0, y: 300)]
        animation.duration = 3
        animation.keyTimes = [0.3,0.8,1]
        redView.layer.add(animation, forKey: nil)
    }
    
    func test6() -> Void {
        let animation = CABasicAnimation(keyPath: "transform")
        animation.duration = 2
        animation.fromValue = 0
        animation.toValue = Double.pi
        animation.valueFunction = CAValueFunction.init(name: .rotateZ)
        redView.layer.add(animation, forKey: nil)
    }
    
    func test5() -> Void {
        let animation = CABasicAnimation(keyPath: "backgroundColor")
        animation.duration = 2
        animation.fromValue = UIColor.blue.cgColor
        animation.toValue = UIColor.orange.cgColor
        redView.layer.add(animation, forKey: nil)
    }
    
    /*
     * 文本绘制层
     */
    func test4() -> Void {
        let layer = CATextLayer()
        layer.backgroundColor = UIColor.lightGray.cgColor
        layer.bounds = CGRect(x: 0, y: 0, width: view.frame.width, height: 100)
        layer.string = "Hello World"
        layer.fontSize = 27
        layer.alignmentMode = .center
        layer.position = CGPoint(x: view.frame.width / 2, y: 300)
        layer.foregroundColor = UIColor.red.cgColor
        
        view.layer.addSublayer(layer)
    }
    
    /*
     * 图形渲染层
     */
    func test3() -> Void {
        let layer = CAShapeLayer()
        //        layer.position = CGPoint(x: 0, y: 0)
        
        let path = CGMutablePath()
        path.move(to: CGPoint(x: 100, y: 200))
        path.addLine(to: CGPoint(x: 300, y: 200))
        path.addLine(to: CGPoint(x: 200, y: 300))
        path.addLine(to: CGPoint(x: 100, y: 200))
        
        layer.path = path
        layer.lineWidth = 4
        
        layer.strokeStart = 0.5
        //        layer.strokeEnd = 0.3
        layer.fillColor = UIColor.red.cgColor
        layer.strokeColor = UIColor.black.cgColor
        view.layer.addSublayer(layer)
    }
    
    /*
     * 色彩梯度层
     */
    func test2() -> Void {
        let layer = CALayer()
        layer.position = CGPoint(x: 100, y: 200)
        layer.backgroundColor = UIColor.red.cgColor
        layer.bounds = CGRect(x: 0, y: 0, width: 50, height: 50)
        
        let caLayer = CAReplicatorLayer()
        caLayer.instanceRedOffset = -0.2
        caLayer.position = CGPoint(x: 0, y: 0)
        caLayer.instanceTransform = CATransform3DMakeTranslation(100, 0, 0)
        caLayer.instanceCount = 3
        caLayer.addSublayer(layer)
        
        view.layer.addSublayer(caLayer)
    }
    
    /*
     * 色彩梯度层
     */
    func test1() -> Void {
        let layer = CAGradientLayer()
        layer.position = CGPoint(x: 100, y: 200)
        layer.colors = [UIColor.red.cgColor,UIColor.blue.cgColor]
        layer.bounds = CGRect(x: 100, y: 100, width: 100, height: 100)
        layer.locations = [0.2]
        layer.startPoint = CGPoint(x: 0, y: 0)
        layer.endPoint = CGPoint(x: 1, y: 1)
        view.layer.addSublayer(layer)
    }
}
